/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: vos_vfs.h
 *
 * Purpose: virtual os virtual file sytem interface define 
 *
 * Developer:
 *   wen.gu , 2015-10-13
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __VOS_VFS_H__
#define __VOS_VFS_H__


#include "general_type.h"

#ifdef __cplusplus 
extern "C"{
#endif

/***************************************************************************
*
* macro declaration
*
***************************************************************************/

/***************************************************************************
*
* data struct declaration
*
***************************************************************************/
// vfs open mode declaration
// R: read, W: write, C:create if file isn't exists, T: truncate if exists,
// A: only write to file end
typedef enum vfs_open_mode_e
{
    VFS_MODE_R = 0,
    VFS_MODE_WCT,
    VFS_MODE_WCA,
    VFS_MODE_RW,
    VFS_MODE_RWCT,
    VFS_MODE_RWCA,
}vfs_open_mode_t;

typedef enum vfs_seek_mode_e
{
    VFS_SEEK_SET = 0,
    VFS_SEEK_CUR,
    VFS_SEEK_END,
}vfs_seek_mode_t;
/***************************************************************************
*
* API declaration
*
***************************************************************************/
/*
 *@brief open a direcotry
 *@param dirPath [in] the path of directory.
 *@return is sucess return directory handle, else return 'NULL'
 *
 **/
G_API GPHD vfsOpenDir(const char* dirPath);

/*
 *@brief close a direcotry
 *@param dirHd [in] the handle of directory.
 *@return None.
 *
 **/
G_API void vfsCloseDir(GPHD dirHd);

/*
 *@brief read a file name in current directory
 *@param dirHd [in] the handle of directory.
 *@return if sucess return file name, if read end return 'NULL'.
 *
 **/
G_API const char* vfsReadFileInDir(GPHD dirHd);

//////////////////////////////////////////////////////////////////////////

/************************************************************************
 *Warning,NOTE: this file API not thread/process safe. 
 ************************************************************************/

/*
 *@brief open a file
 *@param name [in] the name of file(include path).
 *@return if sucess return file handle, else return 'NULL'.
 *
 **/
G_API GPHD vfsFileOpen(const char* name, vfs_open_mode_t mode);

/*
 *@brief close a file
 *@param fhd [in] file handle.
 *@return None.
 *
 **/
G_API void vfsFileClose(GPHD fhd);

/*
 *@brief read data from file
 *@param fhd    [in] file handle
 *@param buf    [io] data buffer
 *@param size   [in] data size
 *@return if sucess return really read size, else return error code.
 *
 *@note if read to file end, will be return '0'  
 *
 **/
G_API GS32 vfsFileRead(GPHD fhd, void* buf, GU32 size);

/*
 *@brief write data to file
 *@param fhd    [in] file handle
 *@param buf    [io] data buffer
 *@param size   [in] data size
 *@return if sucess return really write size, else return error code. 
 *
 **/
G_API GS32 vfsFileWrite(GPHD fhd, const void* buf, GU32 size);

/*
 *@brief move read/write pointer
 *@param fhd    [in] file handle
 *@param offset [in] the write offset in file.
 *@param whenc  [in] seek mode(cur,set, end)
 *@return if sucess return really offset, else return error code. 
 *
 **/
G_API GS64 vfsFileSeek(GPHD fhd, GS64 offset, vfs_seek_mode_t whence);

/*
 *@brief get current read/write pointer offset in  file
 *@param fhd    [in] file handle
 *@return if sucess return current offset, else return error code. 
 *
 **/
G_API GS64 vfsFileTell(GPHD fhd);

/*
 *@brief sync file data
 *@param fhd [in] file handle.
 *@return if sucess return G_OK, else return error code.
 *
 **/
G_API GS32 vfsFileSync(GPHD fhd);

/*
 *@brief get file size
 *@param fhd [in] file handle.
 *@return if sucess return file size, else return '0'.
 *
 **/
G_API GS64 vfsFileSize(GPHD fhd);




#ifdef __cplusplus 
}
#endif

#endif //end of __VOS_VFS_H__
